package dryven.unittest;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.UnsupportedEncodingException;

import dryven.config.ApplicationConfiguration;
import dryven.config.xml.XMLConfigFileParser;
import junit.framework.TestCase;

public class XMLAppConfigTestCase extends TestCase {
	public void testParser() throws UnsupportedEncodingException {
		String xml = "<app-config xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"config.xsd\" environment=\"production\" debug=\"true\" testing=\"false\" preproduction=\"false\" production=\"false\">"+
			"<startup>"+
				"<factory class=\"dryven.init.DefaultFrontControllerFactory\"/>"+
			"</startup>"+
			"<locations>"+
				"<controllers package=\"app.controllers\"/>"+
				"<models package=\"app.models\"/>"+
				"<views package=\"app.views\"/>"+
				"<storage directory=\"/Users/bruno/\"/>"+
			"</locations>"+
			"<database>"+
				"<connections default=\"main\">"+
					"<mysql name=\"main\" user=\"root\" pass=\"password\" server=\"localhost\" port=\"3306\"/>"+
					"<jdbc name=\"\" driver=\"\" url=\"\"/>"+
				"</connections>"+
			"</database>"+
			"<mail>"+
				"<smtp host=\"\" user=\"\" pass=\"\" port=\"25\" ssl=\"false\"/>"+
				"<override-recipient email=\"bruno.windel@gmail.com\"/>"+
				"<subject-prefix text=\"DEBUG: \"/>"+
			"</mail>"+
			"<custom>"+
				"<excel-encoding>ANSI</excel-encoding>"+
				"<bbva>"+
					"<secret-key>LFKLKMALMLAM</secret-key>"+
					"<response-t>bwindels@dryven.org</response-t>"+
				"</bbva>"+
			"</custom>"+
		"</app-config>";
		XMLConfigFileParser parser = new XMLConfigFileParser(new ByteArrayInputStream(xml.getBytes("UTF-8")));
		ApplicationConfiguration cfg = parser.parse();
		assertTrue(cfg.getUserDataDirectory().equals(new File("/Users/bruno/")));
		assertTrue(cfg.getFrontControllerFactoryClass().equals(dryven.init.DefaultFrontControllerFactory.class));
		assertTrue(cfg.getMailSenderConfiguration().getOverrideRecipient().equals("bruno.windel@gmail.com"));
		assertTrue(cfg.getDefaultDatabaseConfiguration().getConnectionName().equals("main"));
		assertTrue(cfg.getDefaultDatabaseConfiguration().getDriverClass().equals("com.mysql.jdbc.Driver"));
		assertTrue(cfg.getDatabaseConfigurationByName("main").getUsername().equals("root"));
		assertTrue(cfg.getDatabaseConfigurationByName("main").getPassword().equals("password"));
		assertTrue(cfg.getModelPackages().iterator().next().equals("app.models"));
		assertTrue(cfg.getControllerPackages().iterator().next().equals("app.controllers"));
		assertTrue(cfg.getViewPackages().iterator().next().equals("app.views"));
	}
}
